<template>
  <div class="category-management">
    <div class="header-with-back">
      <button class="back-btn" @click="$emit('back')">
        <el-icon><ArrowLeft /></el-icon> 返回
      </button>
      <h1 class="page-title">分类管理</h1>
    </div>
    
    <!-- 添加分类按钮 -->
    <div class="actions-bar">
      <button class="btn btn-primary" @click="showAddModal = true">
        <i class="fas fa-plus"></i> 添加分类
      </button>
    </div>

    <!-- 分类列表 -->
    <div class="categories-grid">
      <div v-for="category in categories" 
           :key="category.id" 
           class="category-card">
        <div class="category-header">
          <h3 class="category-name">{{ category.name }}</h3>
        </div>
        <div class="category-body">
          <p class="category-desc">{{ category.description || '暂无描述' }}</p>
          <div class="category-stats">
            <span class="stats-item">
              <el-icon><Reading /></el-icon> {{ category.bookCount || 0 }} 本书籍
            </span>
          </div>
        </div>
        <div class="category-footer">
          <button class="btn btn-icon" @click="editCategory(category)">
            <i class="fas fa-edit"></i>
          </button>
          <button class="btn btn-icon btn-danger" @click="confirmDelete(category)">
            <el-icon><Delete /></el-icon>
          </button>
        </div>
      </div>
    </div>

    <!-- 添加/编辑分类弹窗 -->
    <Modal 
      v-model="showAddModal"
      :title="showEditModal ? '编辑分类' : '添加分类'"
      width="500px"
    >
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label>分类名称</label>
          <input type="text" v-model="formData.name" required />
        </div>
        <div class="form-group">
          <label>描述</label>
          <textarea v-model="formData.description" rows="3"></textarea>
        </div>
      </form>
      <template #footer>
        <button class="btn btn-cancel" @click="closeModal">取消</button>
        <button class="btn btn-primary" @click="handleSubmit">
          {{ showEditModal ? '保存' : '添加' }}
        </button>
      </template>
    </Modal>

    <!-- Toast通知组件 -->
    <Toast 
      :message="toastMessage" 
      :type="toastType" 
      :visible="showToast" 
      @close="showToast = false" 
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Toast from '@/components/Toast.vue'
import Modal from '@/components/Modal.vue'
import api from '@/api/apiClient'
import { Reading, Delete, ArrowLeft } from '@element-plus/icons-vue'

// 定义事件
defineEmits(['back'])

// 状态
const categories = ref([])
const showAddModal = ref(false)
const showEditModal = ref(false)
const formData = ref({
  name: '',
  description: ''
})
const editingId = ref(null)

// Toast状态
const showToast = ref(false)
const toastMessage = ref('')
const toastType = ref('info')

// 获取分类列表
const fetchCategories = async () => {
  try {
    const response = await api.category.getAll()
    categories.value = response
  } catch (error) {
    showNotification('获取分类列表失败', 'error')
  }
}

// 显示通知
const showNotification = (message, type = 'info') => {
  toastMessage.value = message
  toastType.value = type
  showToast.value = true
}

// 关闭弹窗
const closeModal = () => {
  showAddModal.value = false
  showEditModal.value = false
  formData.value = { name: '', description: '' }
  editingId.value = null
}

// 编辑分类
const editCategory = (category) => {
  formData.value = {
    name: category.name,
    description: category.description
  }
  editingId.value = category.id
  showEditModal.value = true
  showAddModal.value = true
}

// 确认删除
const confirmDelete = async (category) => {
  if (confirm(`确定要删除分类"${category.name}"吗？`)) {
    try {
      await api.category.delete(category.id)
      await fetchCategories()
      showNotification('删除分类成功', 'success')
    } catch (error) {
      showNotification('删除分类失败', 'error')
    }
  }
}

// 处理表单提交
const handleSubmit = async () => {
  try {
    if (showEditModal.value) {
      await api.category.update(editingId.value, formData.value)
      showNotification('更新分类成功', 'success')
    } else {
      await api.category.create(formData.value)
      showNotification('添加分类成功', 'success')
    }
    await fetchCategories()
    closeModal()
  } catch (error) {
    showNotification(
      showEditModal.value ? '更新分类失败' : '添加分类失败', 
      'error'
    )
  }
}

onMounted(() => {
  fetchCategories()
})
</script>

<style scoped>
.category-management {
  width: 100%;
}

.page-title {
  margin-bottom: 1rem;
  font-size: 1.8rem;
  color: #374151;
}

.actions-bar {
  margin-bottom: 2rem;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.category-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 1.5rem;
}

.category-header {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.category-name {
  font-size: 1.25rem;
  font-weight: 600;
  color: #111827;
  margin: 0;
}

.category-body {
  margin-bottom: 1rem;
}

.category-desc {
  color: #6b7280;
  font-size: 0.875rem;
  margin-bottom: 0.75rem;
}

.category-stats {
  color: #4b5563;
  font-size: 0.875rem;
}

.stats-item {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.category-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.btn {
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-primary {
  background-color: #3b82f6;
  color: white;
}

.btn-icon {
  padding: 0.5rem;
  border-radius: 0.375rem;
  background: #f3f4f6;
  color: #6b7280;
  transition: all 0.2s;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon i {
  font-size: 1rem;
}

.btn-danger {
  background: #fee2e2;
  color: #ef4444;
}

.btn-danger:hover {
  background: #fecaca;
  opacity: 1;
}

.btn:hover {
  opacity: 0.9;
}

/* Modal styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
}

.modal-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-btn {
  background: #f3f4f6;
  border: none;
  color: #4b5563;
  cursor: pointer;
  padding: 0.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 4px;
  transition: all 0.2s;
}

.close-icon {
  font-size: 16px;
}

.close-btn:hover {
  background-color: #e5e7eb;
  color: #1f2937;
}

.modal-body {
  padding: 1.5rem;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid #e5e7eb;
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #374151;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #d1d5db;
  border-radius: 0.375rem;
  font-size: 0.875rem;
}

@media (max-width: 768px) {
  .category-management {
    padding: 1rem;
  }
  
  .categories-grid {
    grid-template-columns: 1fr;
  }
}

.header-with-back {
  display: flex;
  align-items: center;
  margin-bottom: 2rem;
}

.back-btn {
  display: flex;
  align-items: center;
  background: none;
  border: none;
  color: #3b82f6;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  padding: 0.5rem 0.75rem;
  margin-right: 1rem;
  border-radius: 4px;
}

.back-btn:hover {
  background-color: #eff6ff;
}

.back-btn .el-icon {
  margin-right: 0.25rem;
}
</style>
